/**
 * Created by IntelliJ IDEA.
 * User: 12
 * Date: 12-5-3
 * Time: 上午9:29
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    $('#add').dialog({
        title:'添加',
        width:480,
        height:390,
        modal:true,
        closed:true,
        //draggable:false,
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:okHandler
        },{
            text:'取消',
            handler:function(){
                $('#add').dialog('close');
            }
        }]
    });

    function okHandler(){
        $('#add_form').form('submit',{
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(data) {
                var data = eval('('+data+')');
                if (data.status == '1'){
                    $('#add').dialog('close');      // close the dialog
                    $('#tt').datagrid('reload');    // reload the user data
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: data.msg
                    });
                }
            }

        });
    };
    $('#tt').datagrid({
        title:'用户列表',
        singleSelect:true,
        idField:'id',
        pagination:true,
        columns:[[
            {field:'id',title:'ID',width:40},
            {field:'name',title:'名称',width:120},
            {field:'username',title:'用户名',width:100},
            {field:'roles',title:'角色',width:160},
            {field:'departmentName',title:'部门',width:160},
            {field:'accountExpired',title:'过期',width:50},
            {field:'accountLocked',title:'锁定',width:50},
            {field:'passwordExpired',title:'密码过期',width:60},
            {field:'enabled',title:'启用',width:50},
            {field:'opt',title:'操作',width:100,align:'center',
                formatter:function(value,rec){
                    return '<span class="operator_lab datagrid_edit">编辑</span><span class="operator_lab datagrid_delete">删除</span>';
                }
            }
        ]],
        toolbar:[{
            text:'添加',
            iconCls:'icon-add',
            handler:function(){
                $('#add').dialog('open');
                $('#add_form').form("clear");

            }
        }],
        onBeforeLoad:function(){
            $(this).datagrid('rejectChanges');
        }
    });

    $(".datagrid_edit").live('click',function(){
        var selected = $('#tt').datagrid('getSelected');

        if (selected){
            $('#add_form').form("clear");
            $('#add').dialog('open').dialog('setTitle','编辑');
            $('#add_form').form('load',selected);
            var authorities = selected.authorities;
            var rolesChks = $("#add_form input[name='role']");
            rolesChks.each(function(){
                var val = $(this).val();
                if($.inArray(val,authorities) != -1) {
                    $(this).attr("checked",true);
                }else{
                    $(this).attr("checked",false);
                }
            });
            $("#accountExpired").attr("checked",selected.accountExpired) ;
            $("#accountLocked").attr("checked",selected.accountLocked) ;
            $("#enabled").attr("checked",selected.enabled) ;
            $("#passwordExpired").attr("checked",selected.passwordExpired) ;
            $("#departmentIdCombo").combotree('setValue', selected.departmentId);
        }
    }) ;
    $(".datagrid_delete").live('click',function(){
        var row = $('#tt').datagrid('getSelected');
        if (row){
            $.messager.confirm('提示','确定删除该记录?',function(r){
                if (r){
                    $.post($('#deleteUrl').val(),{id:row.id},function(result){
                        if (result.status == '1'){
                            $('#tt').datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    },'json');
                }
            });
        }

    } )
})

